﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class SystemManage_SysLogList : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private int pageIndex
    {
        get
        {
            if (Request.Params["page"] == null)
                return 1;
            else
            {
                return int.Parse(Request.Params["page"]);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("100500");
       
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            BindDropOperator();
            BindDropFunction();
            Bind();
        }
    }

    private void BindDropOperator()
    {
        dropOperator.DataSource = new BLL.USERS().GetAllList();
        dropOperator.DataTextField = "USERS_NAME";
        dropOperator.DataValueField = "USERS_CODE";
        dropOperator.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        dropOperator.Items.Insert(0, li);
    }

    private void BindDropFunction()
    {
        List<Model.FUNCTIONS> operations = new BLL.FUNCTIONS().GetAllList();
        dropFunction.DataSource = operations;
        dropFunction.DataTextField = "FUNC_NAME";
        dropFunction.DataValueField = "FUNC_CODE";
        dropFunction.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        dropFunction.Items.Insert(0, li);
    }

    private void Bind()
    {
        int pageCount, Counts;
        string strSql = "";
        strSql += dropOperator.SelectedValue == "-1" ? "" : " and SL_USERCODE='" + dropOperator.SelectedValue + "' ";
        strSql += dropFunction.SelectedValue == "-1" ? "" : " and SL_FUNCCODE='" + dropFunction.SelectedValue + "' ";
        strSql += txtStartDate.Text.Trim() == "" ? "" : " and SL_TIME >='" + txtStartDate.Text.Trim() + "' ";
        strSql += txtEndDate.Text.Trim() == "" ? "" : " and SL_TIME <='" + txtEndDate.Text.Trim() + " 23:59:59:999' ";

        DataSet ds = new BLL.SYSLOG().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "[SYSLOG] LEFT JOIN [USERS] ON SL_USERCODE=USERS_CODE LEFT JOIN [FUNCTIONS] ON SL_FUNCCODE=FUNC_CODE LEFT JOIN [OPERATION] ON SL_OPCODE=OP_CODE", "SYSLOG.SL_ID,SYSLOG.SL_TIME,USERS.USERS_NAME,FUNCTIONS.FUNC_NAME,OPERATION.OP_NAME", "SL_TIME", "SL_ID", true, strSql, out pageCount, out Counts);
        SYSLOGListControl1.Bind(ds);
        AspNetPager1.RecordCount = Counts;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        Bind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void btnClearLogBeforeOneMonth_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("100500", "00003"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        DateTime now = DateTime.Now;
        DateTime date = now.AddDays(-30.00);
        if (new BLL.SYSLOG().ClearLogBeforeOneMonth(date))
        {
            Share.InsertLog("100500", "00003");
            Share.Alert(this.Page, "清除成功！");
        }
        else
        {
            Share.Alert(this.Page,"清除失败");
        }
    }
}
